package com.yunfeisoft.business.service.impl;

import com.yunfeisoft.base.model.WorkflowInstance;
import com.yunfeisoft.business.dao.inter.GoodsUseApprovalDao;
import com.yunfeisoft.business.model.GoodsUseApproval;
import com.yunfeisoft.business.service.inter.GoodsUseApprovalService;
import com.yunfeisoft.common.base.BaseServiceImpl;
import com.yunfeisoft.common.sql.separation.DataSourceChange;
import com.yunfeisoft.common.utils.DateUtils;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.utils.WorkflowHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * ClassName: GoodsUseApprovalServiceImpl
 * Description: 物品领用审批表service实现
 * Author: Jackie liu
 * Date: 2020-05-14
 */
@Service("goodsUseApprovalService")
public class GoodsUseApprovalServiceImpl extends BaseServiceImpl<GoodsUseApproval, String, GoodsUseApprovalDao> implements GoodsUseApprovalService {

    @Autowired
    private WorkflowHelper workflowHelper;

    @Override
    @DataSourceChange(slave = true)
    public Page<GoodsUseApproval> queryPage(Map<String, Object> params) {
        return getDao().queryPage(params);
    }

    @Override
    public int modifyStatus(String id, int status) {
        if (StringUtils.isBlank(id)) {
            return 0;
        }
        GoodsUseApproval record = new GoodsUseApproval();
        record.setId(id);
        record.setStatus(status);
        return getDao().update(record);
    }

    @Override
    public int save(GoodsUseApproval goodsUseApproval) {
        int result = super.save(goodsUseApproval);

        WorkflowInstance instance = new WorkflowInstance();
        instance.setWorkflowDefineId(goodsUseApproval.getWorkflowDefineId());
        instance.setFormId(goodsUseApproval.getId());
        instance.setTitle("物品领用审批单:" + DateUtils.getNowTime());

        workflowHelper.saveAndStartProcess(instance, goodsUseApproval.getNextStepUserId());

        return result;
    }
}